package edu.gdkm.paydemo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CsvReader {
    private String filePath;
    private char delimiter; // 分隔符，例如逗号 ',' 或分号 ';'

    public CsvReader(String filePath, char delimiter) {
        this.filePath = filePath;
        this.delimiter = delimiter;
    }

    public Student[] readCsv() throws IOException {
        List<Student> csvDataList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            boolean isHeader = true; // 跳过标题行
            while ((line = br.readLine()) != null) {
                if (isHeader) {
                    isHeader = false; // 读取下一行前跳过标题行
                    continue;
                }
                String[] values = line.split(String.valueOf(delimiter));
                if (values.length >= 2) { // 确保至少有2列数据，根据实际需要调整
                    Student csvData = new Student(values[0].trim(), values[1].trim(),Double.parseDouble(values[2]), Double.parseDouble(values[3]), Double.parseDouble(values[4]));
                    csvDataList.add(csvData);
                } else {
                    // 处理格式错误的行，例如可以记录日志或抛出异常
                }
            }
        }
        return csvDataList.toArray(new Student[csvDataList.size()]);
    }
}
